package xmlcollabclient.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Sasa
 */
public class Login {

    private String username = "salebab";
    private String password = "test";
    public Boolean isMember = false;

    public JFrame f;
    public JPanel jPanelUser;
    public JLabel jLabelUser;
    public JPanel jPanelPass;
    public JLabel jLabelPass;

    public JPanel jPanelLogin;
    public JSeparator jSeparator1;


    public Login() {
        jPanelUser = new JPanel();
        jLabelUser = new JLabel();
        jPanelPass = new JPanel();
        jLabelPass = new JLabel();
        jPanelLogin = new JPanel();
        jSeparator1 = new JSeparator();

        f = new JFrame("Collab - Login");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBounds(new Rectangle(400, 200, 335, 175));
        f.setResizable(false);
        f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));

        jPanelUser.setLayout(new FlowLayout(FlowLayout.LEFT));

        jLabelUser.setFont(new Font("Verdana", 1, 14));
        jLabelUser.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabelUser.setText("Username:");
        jLabelUser.setHorizontalTextPosition(SwingConstants.LEADING);
        jLabelUser.setMaximumSize(new Dimension(100, 20));
        jLabelUser.setMinimumSize(new Dimension(100, 20));
        jLabelUser.setPreferredSize(new Dimension(100, 20));
        jPanelUser.add(jLabelUser);

        final JTextField jTextFieldUser = new JTextField();
        jTextFieldUser.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jTextFieldUser.setMargin(new java.awt.Insets(1, 5, 1, 1));
        jTextFieldUser.setMaximumSize(new java.awt.Dimension(200, 40));
        jTextFieldUser.setMinimumSize(new java.awt.Dimension(200, 40));
        jTextFieldUser.setPreferredSize(new java.awt.Dimension(200, 40));
        jPanelUser.add(jTextFieldUser);

        f.getContentPane().add(jPanelUser);

        jPanelPass.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabelPass.setFont(new java.awt.Font("Verdana", 1, 14));
        jLabelPass.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelPass.setText("Password:");
        jLabelPass.setMaximumSize(new java.awt.Dimension(100, 20));
        jLabelPass.setMinimumSize(new java.awt.Dimension(100, 20));
        jLabelPass.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanelPass.add(jLabelPass);

        final JPasswordField jTextFieldPass = new JPasswordField();
        jTextFieldPass.setFont(new java.awt.Font("Tahoma", 0, 24));
        jTextFieldPass.setMargin(new java.awt.Insets(1, 5, 1, 1));
        jTextFieldPass.setMaximumSize(new java.awt.Dimension(200, 40));
        jTextFieldPass.setMinimumSize(new java.awt.Dimension(200, 40));
        jTextFieldPass.setPreferredSize(new java.awt.Dimension(200, 40));
        jPanelPass.add(jTextFieldPass);

        f.getContentPane().add(jPanelPass);
        f.getContentPane().add(jSeparator1);

        JButton jButtonLogin = new JButton("Login");
        jButtonLogin.setText("Login");
        jPanelLogin.add(jButtonLogin);

        f.getContentPane().add(jPanelLogin);

        jButtonLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                doLogin(jTextFieldUser.getText(), jTextFieldPass.getText());
            }
        });
        
        f.setVisible(true);
    }

    public void doLogin(String usr, String psw) {
        
        if(check(usr, psw)) {
            isMember = true;
            f.dispose();
            f.setVisible(isMember);
        } else {
            JOptionPane.showMessageDialog(new JFrame(), "Login Error. Please, don't try again.");
        }        
    }

    private Boolean check(String usr, String psw) {
        return (usr.equals(username) && psw.equals(password)) ? true : false;
    }
}
